/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    // 寻找中间的节点
    ListNode* middleNode(ListNode* head) {
        ListNode* fast = head;
        ListNode* slow = head;
        while (fast&&fast->next) {
            fast = fast->next->next;
            slow = slow->next;
        }
        return slow;
    }

    // 翻转链表
    ListNode* reverseNode(ListNode* head) {
        ListNode* prev = nullptr;
        ListNode* cur = head;
        while (cur) {
            ListNode* next = cur->next;
            cur->next = prev;
            prev = cur;
            cur = next;
        }
        return prev;
    }
    bool isPalindrome(ListNode* head) {
        ListNode* midNode=middleNode(head);
        ListNode* head2=reverseNode(midNode);
        while(head2)
        {
            if(head->val!=head2->val) return false;
            head=head->next;
            head2=head2->next;  
        }
        return true;
    }
};
